﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;

namespace FarmSmartSleep.Organ.API.Application.Commands.CottagesCommands
{
    /// <summary>
    /// 栋舍查询
    /// </summary>
    public class CottageSearchHander : IRequestHandler<CottageSearchCommand, ResultPage<CottageListDto>>
    {

        private readonly IBaseRepository<Cottages> cottageRepository;
        private readonly IBaseRepository<CottageType> cottageTypeRepository;
        private readonly IBaseRepository<Employee> employeeRepository;
        private readonly IBaseRepository<SheepFarm> sheepFarmRepository;
        private readonly ILogger<CottageSearchHander> logger;

        public CottageSearchHander(IBaseRepository<Cottages> cottageRepository, IBaseRepository<CottageType> cottageTypeRepository, IBaseRepository<Employee> employeeRepository, ILogger<CottageSearchHander> logger, IBaseRepository<SheepFarm> sheepFarmRepository)
        {
            this.cottageRepository = cottageRepository;
            this.cottageTypeRepository = cottageTypeRepository;
            this.employeeRepository = employeeRepository;
            this.logger = logger;
            this.sheepFarmRepository = sheepFarmRepository;
        }

        /// <summary>
        /// 栋舍查询方法（分页）
        /// </summary>
        /// <param name="request">栋舍查询方法请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>栋舍查询方法响应对象</returns>
        public async Task<ResultPage<CottageListDto>> Handle(CottageSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultPage<CottageListDto>();
                var list = (from a in cottageRepository.GetAll()
                            join b in cottageTypeRepository.GetAll()
                            on a.CottageTypeId equals b.CottageTypeId
                            join c in employeeRepository.GetAll()
                            on a.PersId equals c.EmployeeId
                            join d in employeeRepository.GetAll()
                            on a.Production equals d.EmployeeId
                            join e in sheepFarmRepository.GetAll()
                            on a.FarmId equals e.FarmId
                            join f in employeeRepository.GetAll()
                            on a.CreateId equals f.EmployeeId
                            //join g in employeeRepository.GetAll()
                            //on a.UpdateId equals g.EmployeeId
                            //join h in employeeRepository.GetAll()
                            //on a.DeleteId equals h.EmployeeId
                            select new CottageListDto
                            {
                                CottagesId = a.CottagesId,
                                CottageName = a.CottageName,
                                CottageTypeId = a.CottageTypeId,
                                PersId = a.PersId,
                                FarmId = a.FarmId,
                                Production = a.Production,
                                Remark = a.Remark,
                                Status = a.Status,
                                CottageTypeName = b.CottageTypeName,
                                PersName = c.FirstName,
                                SheepFarmNo = e.FarmName,
                                ProductionName = d.FirstName,
                                CreateTime = a.CreateTime,
                                CreateId = a.CreateId,
                                UpdateId = a.UpdateId,
                                UpdateTime = a.UpdateTime,
                                DeleteId = a.DeleteId,
                                DeleteTime = a.DeleteTime,
                                Deletes = a.Deletes,
                                CreateName = f.FirstName,
                                //UpdateName = g.FirstName,
                                //DeleteName = h.FirstName,

                            }).ToList();

                foreach (var item in list)
                {
                    if (item.UpdateId > 0)
                    {
                        item.UpdateName = employeeRepository.GetAll().FirstOrDefault(c => c.EmployeeId == item.UpdateId).FirstName;
                    }
                    if (item.DeleteId > 0)
                    {
                        item.DeleteName = employeeRepository.GetAll().FirstOrDefault(c => c.EmployeeId == item.DeleteId).FirstName;
                    }
                }

                if (!string.IsNullOrEmpty(request.CottageName))
                {
                    list = list.Where(c => c.CottageName.Contains(request.CottageName)).ToList();
                }
                if (request.PersId > 0)
                {
                    list = list.Where(c => c.PersId == request.PersId).ToList();
                }
                if (request.CottageTypeId > 0)
                {
                    list = list.Where(c => c.CottageTypeId == request.CottageTypeId).ToList();
                }
                if (request.PageIndex != 0)
                {
                    result.totalCount = list.Count();
                    result.totalPage = (int)Math.Ceiling((result.totalCount) * 1.0 / request.PageSize);
                    result.Data = list.OrderBy(c => c.CottagesId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                }
                else
                {
                    result.Data = list;
                }
                logger.LogInformation("栋舍查询方法调用成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("栋舍查询方法调用出错" + ex);
                throw;
            }

        }
    }


    /// <summary>
    /// 栋舍分页显示查询Dto
    /// </summary>
    public class CottageListDto
    {

        /// <summary>
        /// 主键id
        /// </summary>
        [Key]
        public int CottagesId { get; set; }
        /// <summary>
        /// 所在羊场
        /// </summary>
        public int FarmId { get; set; }
        /// <summary>
        /// 栋舍类型
        /// </summary>
        public int CottageTypeId { get; set; }
        /// <summary>
        /// 栋舍名称
        /// </summary>
        public string CottageName { get; set; }
        /// <summary>
        /// 负责人
        /// </summary>
        public int PersId { get; set; }
        /// <summary>
        /// 生产人员
        /// </summary>
        public int Production { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public bool Status { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        [StringLength(100)]
        public string? Remark { get; set; }
        /// <summary>
        /// 负责人名
        /// </summary>
        public string PersName { get; set; }
        /// <summary>
        /// 生产人员名称
        /// </summary>
        public string ProductionName { get; set; }
        /// <summary>
        /// 羊场名称
        /// </summary>
        public string? SheepFarmNo { get; set; }
        /// <summary>
        /// 栋舍类型名称
        /// </summary>
        public string? CottageTypeName { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public int CreateId { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 修改人
        /// </summary>
        public int? UpdateId { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 删除人
        /// </summary>
        public int? DeleteId { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        public bool Deletes { get; set; }
        /// <summary>
        /// 删除时间
        /// </summary>
        public DateTime? DeleteTime { get; set; }

        /// <summary>
        /// 创建人姓名
        /// </summary>
        public string? CreateName { get; set; }
        /// <summary>
        /// 修改人姓名
        /// </summary>
        public string? UpdateName { get; set; }
        /// <summary>
        /// 删除人姓名
        /// </summary>
        public string? DeleteName { get; set; }


    }
}
